/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Feed {
    private String id;

    private Title title;

    private String updated;

    private String published;

    private Collection<Author> authors;

    private Collection<Link> links;

    private String endIndex;

    private List<Entry> entries;

    private Category category;

    private Contributor contributor;

    private String icon;

    private String logo;

    private Rights rights;

    private Title subtitle;

    private Generator generator;

    public Generator getGenerator() {
        return generator;
    }

    public void setGenerator(Generator generator) {
        this.generator = generator;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public Rights getRights() {
        return rights;
    }

    public void setRights(Rights rights) {
        this.rights = rights;
    }

    public Title getSubtitle() {
        return subtitle;
    }

    public void setSubtitle(Title subtitle) {
        this.subtitle = subtitle;
    }

    public Feed() {
        links = new ArrayList<Link>();
        entries = new ArrayList<Entry>();
        authors = new ArrayList<Author>();
    }

    public void addAuthor(Author author) {
        authors.add(author);
    }

    public void addEntry(Entry entry) {
        entries.add(entry);
    }

    public void addLink(Link link) {
        links.add(link);
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Contributor getContributor() {
        return contributor;
    }

    public void setContributor(Contributor contributor) {
        this.contributor = contributor;
    }

    public String getPublished() {
        return published;
    }

    public void setPublished(String published) {
        this.published = published;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Title getTitle() {
        return title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }

    public String getUpdated() {
        return updated;
    }

    public void setUpdated(String updated) {
        this.updated = updated;
    }

    public Collection<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(Collection<Author> authors) {
        this.authors = authors;
    }

    public Collection<Link> getLinks() {
        return links;
    }

    public void setLinks(Collection<Link> links) {
        this.links = links;
    }

    public String getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(String endIndex) {
        this.endIndex = endIndex;
    }

    public List<Entry> getEntries() {
        return entries;
    }

    public void setEntries(List<Entry> entries) {
        this.entries = entries;
    }

    public static Feed marshalFrom(KXmlParser parser, AtomEntityContext ctx) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }

        Feed feed = new Feed();
        try {
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "feed");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if (tagName.equals("id")) {
                    feed.setId(parser.nextText());
                } else if (tagName.equals("title")) {
                    feed.setTitle(Title.marshalFrom(parser, ctx));
                } else if (tagName.equals("author")) {
                    feed.addAuthor(Author.marshalFrom(parser));
                } else if (tagName.equals("link")) {
                    feed.addLink(Link.marshalFrom(parser, ctx));
                } else if (tagName.equals("updated")) {
                    feed.setUpdated(parser.nextText());
                } else if (tagName.equals("as:endIndex")) {
                    feed.setEndIndex(parser.nextText());
                } else if (tagName.equals("entry")) {
                    feed.addEntry(Entry.marshalFrom(parser, ctx));
                } else if (tagName.equals("published")) {
                    feed.setEndIndex(parser.nextText());
                } else if (tagName.equals("category")) {
                    feed.setCategory(Category.marshalFrom(parser, ctx));
                } else if (tagName.equals("contributor")) {
                    feed.setContributor(Contributor.marshalFrom(parser));
                } else if (tagName.equals("icon")) {
                    feed.setIcon(parser.nextText());
                } else if (tagName.equals("logo")) {
                    feed.setLogo(parser.nextText());
                } else if (tagName.equals("rights")) {
                    feed.setRights(Rights.marshalFrom(parser, ctx));
                } else if (tagName.equals("subtitle")) {
                    feed.setSubtitle(Title.marshalFrom(parser, ctx));
                } else if (tagName.equals("generator")) {
                    feed.setGenerator(Generator.marshalFrom(parser, ctx));
                }
            }
        } catch (XmlPullParserException e) {
            throw new IOException("SLIDE-000-010:" + e.toString());
        }

        if (feed.getTitle() == null) {
            throw new IOException("Missing required title tag");
        }

        if (feed.getId() == null) {
            throw new IOException("Missing required id tag");
        }

        if (feed.getUpdated() == null) {
            throw new IOException("Missing required updated tag");
        }

        return feed;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("Id = ").append(id).append("\r\n");
        sb.append(title).append("\r\n");
        sb.append(subtitle).append("\r\n");
        sb.append("Updated = ").append(updated).append("\r\n");
        sb.append("Published = ").append(published).append("\r\n");
        sb.append("Entry Count = ").append(entries.size());
        return sb.toString();
    }
}
